package org.example.k8s_cloudplatform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.example.k8s_cloudplatform.entity.Pod;
import org.example.k8s_cloudplatform.entity.PodRequest;
import org.example.k8s_cloudplatform.entity.res.Result;
import org.example.k8s_cloudplatform.service.PodRequestService;
import org.example.k8s_cloudplatform.service.PodService;
import org.example.k8s_cloudplatform.utils.JwtUtil;
import org.example.k8s_cloudplatform.utils.K8S_util;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/podRequest")
public class PodRequestController {
    @Resource
    private PodRequestService podRequestService;
    @Resource
    private PodService podService;

    @PostMapping("/request")
    public Result createPodRequest(@RequestBody PodRequest podRequest,@RequestHeader("Token") String token) {
        Integer id= JwtUtil.IDParser(token);
        podRequest.setStatus(0);
        podRequest.setType(0);
        podRequest.setUserID(id);
        if(!K8S_util.isValidPodName(podRequest.getPodName())){
            return Result.fail("Docker名必须由小写字母数字字符'-'或'组成.'，且以字母数字字符开头和结尾",null);
        }
        LambdaQueryWrapper<Pod> podWrapper= Wrappers.lambdaQuery();
        podWrapper.eq(Pod::getUserID,id);
        podWrapper.eq(Pod::getStatus,0);
        podWrapper.eq(Pod::getName,podRequest.getPodName());
        if(podService.count(podWrapper)>0){
            return Result.fail("您已存在该名称的Docker，不允许重复命名",null);
        }
        LambdaQueryWrapper<PodRequest> podRequestWrapper= Wrappers.lambdaQuery();
        podRequestWrapper.eq(PodRequest::getUserID,id);
        podRequestWrapper.eq(PodRequest::getStatus,0);
        podRequestWrapper.eq(PodRequest::getPodName,podRequest.getPodName());
        if(podRequestService.count(podRequestWrapper)>0){
            return Result.fail("您已存在该名称的Docker申请，请耐心等待运维人员处理",null);
        }
        podRequestService.save(podRequest);
        return Result.success();
    }

    @GetMapping("/relet")
    public Result reletPodRequest(@RequestParam Integer dockerID, @RequestParam Integer applyTime) {
        return podRequestService.relet(dockerID,applyTime);
    }

    @GetMapping("/viewMyRequest")
    public Result viewMyRequest(@RequestHeader("Token") String token,@RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize) {
        Integer id= JwtUtil.IDParser(token);
        LambdaQueryWrapper<PodRequest> podRequestWrapper=Wrappers.lambdaQuery();
        podRequestWrapper.eq(PodRequest::getUserID,id);
        podRequestWrapper.orderByDesc(PodRequest::getID);//由于自增ID，所以越新的越靠后

        Page<PodRequest> podRequestPage=podRequestService.page(new Page<>(pageNum, pageSize), podRequestWrapper);
        return Result.success(podRequestPage);
    }
    @GetMapping("/viewUnprocessedRequest")
    public Result viewUnprocessedRequest(@RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<PodRequest> podRequestWrapper=Wrappers.lambdaQuery();
        podRequestWrapper.eq(PodRequest::getStatus,0);

        Page<PodRequest> podRequestPage=podRequestService.page(new Page<>(pageNum, pageSize), podRequestWrapper);
        return Result.success(podRequestPage);
    }
    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestBody PodRequest podRequest) {
        return podRequestService.updateStatus(podRequest);
    }
}
